package com.meilai.project.controller.web.business.media;


import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.business.installOrder.MediaManualLockDTO;
import com.meilai.project.dto.system.ImportDeviceByCodeDTO;
import com.meilai.project.dto.system.ImportDeviceByCodeDelDTO;
import com.meilai.project.mapper.business.media.MediaTypeMapper;
import com.meilai.project.service.business.media.ImportDeviceByCodeService;
import com.meilai.project.service.business.media.MediaPointService;
import com.meilai.project.service.media.MediaPointOccupySnapshotService;
import com.meilai.project.vo.business.installOrder.MediaPointLockDateRangeVO;
import com.meilai.project.vo.business.media.MediaPointCountVO;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

@Api(value = "媒体编号控制器", description = "媒体编号相关")
@Log4j2
@RestController
@RequestMapping("api/web/media")
public class MediaController {

    @Autowired
    private ImportDeviceByCodeService importDeviceByCodeService;

    @Autowired
    private MediaPointOccupySnapshotService mediaPointOccupySnapshotService;

    @Autowired
    private MediaPointService mediaPointService;

    @Autowired
    private MediaTypeMapper mediaTypeMapper;

    @GetMapping("list-media-type-include-proxy")
    @ApiOperation("查询包含代理在内的所有的媒体类型")
    public ApiResult<List<String>> listMediaTypeIncludeProxy() {
        return ApiResult.ok(mediaTypeMapper.queryAllIncludeProxy());
    }

    @GetMapping("list-lock-media-id")
    @ApiOperation("根据点位id查询锁定区间")
    public ApiResult<List<MediaPointLockDateRangeVO>> listLockMediaId(
            @ApiParam("mediaId") @RequestParam Long mediaId) {
        return ApiResult.ok(mediaPointOccupySnapshotService.listOccupyByMediaId(mediaId));
    }

    @PostMapping("submit-manual-lock")
    @ApiOperation("手动锁定点位")
    public ApiResult<Boolean> submitManualLock(
            @ApiParam("json") @RequestBody MediaManualLockDTO req) {
        mediaPointOccupySnapshotService.submitManualLock(req);
        return ApiResult.ok();
    }

    @DeleteMapping("release-manual-lock")
    @ApiOperation("释放手动锁定")
    public ApiResult<Boolean> releaseManualLock(
            @ApiParam("chooseId") @RequestParam Long chooseId) {
        mediaPointOccupySnapshotService.releaseManualLockManualLock(chooseId);
        return ApiResult.ok();
    }

    @PutMapping("delete")
    @ApiOperation(value = "删除入库编号")
    public ApiResult<Boolean> setRole(@RequestBody ImportDeviceByCodeDelDTO dto) {
        return ApiResult.ok(importDeviceByCodeService.delOne(dto));
    }

    @PostMapping("import_code")
    @ApiOperation(value = "导入设备编号")
    public CommonResult importCode(@RequestBody ImportDeviceByCodeDTO dto) {
        importDeviceByCodeService.validDTO(dto);
        Long record_id = importDeviceByCodeService.addRecord(dto);
        try {
            Runnable runnable = () -> {
                importDeviceByCodeService.addOne(dto, record_id);
            };
            new Thread(runnable).start();
        } catch (Exception e) {
            log.error("编号入库失败", e);
        }
        return new CommonResult().success("已提交，请稍后刷新页面");
    }

    @GetMapping("import_by_code_list")
    @ApiOperation(value = "编号入库记录")
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size", defaultValue = "10") Integer page_size) {
        return new CommonResult().success(importDeviceByCodeService.selectPageList(page, page_size));
    }

    @PostMapping("export_leave_unused_point_excel")
    @ApiOperation(value = "导出空位Excel")
    public void exportLeaveUnusedPointExcel(HttpServletResponse response,
                                            @RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                                            @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
                                            @RequestBody List<Long> building_ids) {
        mediaPointService.exportLeaveUnusedPointExcel(response, startDate, endDate, building_ids, null, true, 4);
    }

//    @PostMapping("export_point")
//    @ApiOperation(value = "导出空位Excel")
//    public void exportPoint(HttpServletResponse response, @RequestBody MediaPointExportDTO dto) {
//        mediaPointService.exportLeaveUnusedPointExcel(response, dto.getStartDate(), dto.getEndDate(), dto.getBuilding_ids(), dto.getMedia_type_ids(), dto.getFloor_group(), dto.getAbType());
//    }

    @ApiOperation(value = "点位统计", notes = "点位统计", response = MediaPointCountVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("/page")
    public CommonResult page(@ApiParam(name = "page", value = "page", required = true)
                             @RequestParam(value = "page") Integer page,
                             @ApiParam(name = "page_size", value = "page_size", required = true)
                             @RequestParam(value = "page_size") Integer page_size,
                             @RequestParam(value = "adcode", required = false) String adcode,
                             @ApiParam(name = "code", value = "编号")
                             @RequestParam(value = "code", required = false) String code,
                             @ApiParam(name = "building_area_id", value = "项目ID")
                             @RequestParam(value = "building_area_id", required = false) Long building_area_id,
                             @ApiParam(name = "media_type_id", value = "媒体类型ID")
                             @RequestParam(value = "media_type_id", required = false) Long media_type_id) {
        return new CommonResult().success(mediaPointService.page(page, page_size, code, building_area_id, media_type_id, adcode));
    }
}

